package com.ruoyi.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Processcardmain;
import com.ruoyi.business.service.IProcesscardmainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工艺文件主Controller
 * 
 * @author DL
 * @date 2025-06-29
 */
@RestController
@RequestMapping("/processcardmain")
public class ProcesscardmainController extends BaseController
{
    @Autowired
    private IProcesscardmainService processcardmainService;

    /**
     * 查询工艺文件主列表
     */
    //@RequiresPermissions("business:processcardmain:list")
    @GetMapping("/list")
    public TableDataInfo list(Processcardmain processcardmain)
    {
        startPage();
        List<Processcardmain> list = processcardmainService.selectProcesscardmainList(processcardmain);
        return getDataTable(list);
    }

    @GetMapping("/listProcesscardmainChengXu")
    public TableDataInfo listProcesscardmainChengXu(Processcardmain processcardmain) {
        List<Processcardmain> list = processcardmainService.selectProcesscardmainList(processcardmain);

        // 转换数据格式
        List<Map<String, String>> transformedList = list.stream()
                .map(item -> {
                    List<Map<String, String>> result = new ArrayList<>();

                    // 添加加工程序
                    if (StringUtils.isNotBlank(item.getProcessedProgram())) {
                        result.add(createEntry("加工程序", item.getProcessedProgram()));
                    }

                    // 添加校验程序
                    if (StringUtils.isNotBlank(item.getVerificateProgram())) {
                        result.add(createEntry("校验程序", item.getVerificateProgram()));
                    }

                    // 添加检测程序
                    if (StringUtils.isNotBlank(item.getTestingProgram())) {
                        result.add(createEntry("检测程序", item.getTestingProgram()));
                    }

                    // 可以继续添加其他需要转换的字段

                    return result;
                })
                .flatMap(List::stream) // 将多个List合并为一个Stream
                .collect(Collectors.toList());

        return getDataTable(transformedList);
    }

    // 辅助方法，创建键值对Map
    private Map<String, String> createEntry(String key, String value) {
        Map<String, String> map = new HashMap<>();
        map.put("A", key);
        map.put("B", value);
        return map;
    }

    /**
     * 导出工艺文件主列表
     */
    //@RequiresPermissions("business:processcardmain:export")
    @Log(title = "工艺文件主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Processcardmain processcardmain)
    {
        List<Processcardmain> list = processcardmainService.selectProcesscardmainList(processcardmain);
        ExcelUtil<Processcardmain> util = new ExcelUtil<Processcardmain>(Processcardmain.class);
        util.exportExcel(response, list, "工艺文件主数据");
    }

    /**
     * 获取工艺文件主详细信息
     */
    //@RequiresPermissions("business:processcardmain:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(processcardmainService.selectProcesscardmainByID(ID));
    }

    /**
     * 新增工艺文件主
     */
    //@RequiresPermissions("business:processcardmain:add")
    @Log(title = "工艺文件主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Processcardmain processcardmain)
    {
        return toAjax(processcardmainService.insertProcesscardmain(processcardmain));
    }

    /**
     * 修改工艺文件主
     */
    //@RequiresPermissions("business:processcardmain:edit")
    @Log(title = "工艺文件主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Processcardmain processcardmain)
                {
        if (processcardmain.getWorkState() != null && processcardmain.getWorkState() == 1){
            processcardmainService.updateProcesscardmainWorkState(processcardmain);
        }
        return toAjax(processcardmainService.updateProcesscardmain(processcardmain));
    }

    /**
     * 删除工艺文件主
     */
    //@RequiresPermissions("business:processcardmain:remove")
    @Log(title = "工艺文件主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(processcardmainService.deleteProcesscardmainByIDs(IDs));
    }
}
